import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grab_the_order/app/data/models/log_model.dart';
import 'package:grab_the_order/app/modules/log/log_controller.dart';

class LogView extends GetView<LogController> {
  const LogView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('运行日志'),
        actions: [
          IconButton(
            icon: const Icon(Icons.delete_sweep_outlined),
            tooltip: '清空日志',
            onPressed: controller.clearAllLogs,
          )
        ],
      ),
      body: Obx(() {
        if (controller.logService.logs.isEmpty) {
          return const Center(child: Text("暂无日志记录"));
        }
        return ListView.builder(
          reverse: true, // 让列表从底部开始显示
          itemCount: controller.logService.logs.length,
          itemBuilder: (context, index) {
            final log = controller.logService.logs[index];
            return Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
              child: Text(
                controller.logService.formatLog(log),
                style: TextStyle(color: _getLogColor(log.level), fontSize: 12),
              ),
            );
          },
        );
      }),
    );
  }
  
  Color _getLogColor(LogLevel level) {
    switch (level) {
      case LogLevel.success: return Colors.green;
      case LogLevel.warning: return Colors.orange;
      case LogLevel.error: return Colors.red;
      default: return Colors.black87;
    }
  }
}